package sv.splayer.rk.kuaifa.com.myapplication;

import android.os.Build;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.danikula.videocache.HttpProxyCacheServer;

import org.song.videoplayer.IVideoPlayer;
import org.song.videoplayer.PlayListener;
import org.song.videoplayer.QSVideoView;
import org.song.videoplayer.Util;
import org.song.videoplayer.media.AndroidMedia;
import org.song.videoplayer.media.BaseMedia;
import org.song.videoplayer.media.ExoMedia;
import org.song.videoplayer.media.IjkExoMedia;
import org.song.videoplayer.media.IjkMedia;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private QSVideoView qsVideoView;


    //    String mp4 = "http://resources.kuaifa.tv/uploads/20180711/5f0f69f67f5a08d7cf10791639f6cb0b.mp4";
    String mp4 = "http://resources.kuaifa.tv//upload/a/2018/0424/164623bc52f605.mp4";
    String m3u8 = "http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8";

    String url;
    Class decodeMedia;
    private String proxyUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Util.SET_FULL(this);
        final HttpProxyCacheServer proxy = PlayApp.getProxy(PlayApp.getInstance());
//            proxy.registerCacheListener(this, url);
        proxyUrl = proxy.getProxyUrl(mp4);
        qsVideoView = findViewById(R.id.qs);
        //进入全屏的模式 0横屏 1竖屏 2传感器自动横竖屏 3根据视频比例自动确定横竖屏      -1什么都不做
        qsVideoView.enterFullMode = 3;
        qsVideoView.setPlayListener(new PlayListener() {
            @Override
            public void onStatus(int status) {//播放状态

                Log.e(TAG, "onStatus: " + status);
                if (status == IVideoPlayer.STATE_AUTO_COMPLETE)
                    qsVideoView.setUrl(m3u8);
                qsVideoView.play();
            }


            @Override
            public void onEvent(int what, Integer... extra) {

                Log.e(TAG, "onEvent: " + what + "   ::::" + extra);


            }
        });

        Log.e(TAG, "onCreate: " + proxyUrl);
        play(proxyUrl, ExoMedia.class);
    }


    private void play(String url, Class<? extends BaseMedia> decodeMedia) {
        qsVideoView.release();
        qsVideoView.setDecodeMedia(decodeMedia);
        qsVideoView.setMute(true);
        qsVideoView.setUp(url);
        qsVideoView.play();
        this.url = url;
        this.decodeMedia = decodeMedia;

    }


    @Override
    public void onResume() {
        super.onResume();
        if (flag)
            qsVideoView.play();
        if (position > 0) {
            qsVideoView.seekTo(position);
            position = 0;
        }
    }

    boolean flag;//记录退出时播放状态 回来的时候继续播放
    int position;//记录销毁时的进度 回来继续盖进度播放

    @Override
    public void onPause() {
        super.onPause();
        //暂停
        flag = qsVideoView.isPlaying();
        qsVideoView.pause();
    }


    @Override
    public void onStop() {
        super.onStop();
        //不马上销毁 延时15秒
        handler.postDelayed(runnable, 1000 * 10);
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (qsVideoView.getCurrentState() != IVideoPlayer.STATE_AUTO_COMPLETE)
                position = qsVideoView.getPosition();
            qsVideoView.release();
        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();//销毁
        qsVideoView.release();
    }


}








